﻿using CQRSFramework.Common.Serializer;
using Newtonsoft.Json;
using System;

namespace CQRSFramework.Common.JsonNet
{
    public class NewtonsoftJsonSerializer : IJsonSerializer
    {
        private readonly JsonSerializerSettings _jsonSerializerSettings;
        public NewtonsoftJsonSerializer()
        {
            _jsonSerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None };
        }
        public T Deserialize<T>(string value) where T : class
        {
            return JsonConvert.DeserializeObject<T>(value, _jsonSerializerSettings);
        }

        public T Deserialize<T>(string value, Type type) where T : class
        {
            return JsonConvert.DeserializeObject(value, type, _jsonSerializerSettings) as T;
        }

        public string Serialize(object obj)
        {
            return JsonConvert.SerializeObject(obj, Formatting.Indented, _jsonSerializerSettings);
        }
    }
}
